<template>
	<div class="m-l-50 m-t-30 w-500">
		<el-form ref="form" :model="form" :rules="rules" label-width="110px">
			<el-form-item label="标题" prop="title">
				<el-input v-model.trim="form.title" class="h-40 w-200"></el-input>
      </el-form-item>
			<el-form-item label="上级菜单" prop="pid">
				<el-select v-model="form.pid" placeholder="上级菜单" class="w-200" v-on:change="selectPid()">
					<el-option v-for="item in options" :label="item.title" :value="item.id" :key="item.id"></el-option>
				</el-select>
			</el-form-item>
      <el-form-item v-if="!form.pid" label="模块" prop="module">
        <el-input v-model.trim="form.module" class="h-40 w-200"></el-input>
      </el-form-item>
			<el-form-item label="路径" v-if="show_url">
				<el-input v-model.trim="form.url" class="h-40 w-200" placeholder=""></el-input>
			</el-form-item>
			<el-form-item label="排序">
				<el-input v-model="form.sort" class="h-40 w-200"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="edit('form')" :loading="loading">提交</el-button>
				<el-button @click="goback()">返回</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
  import http from '../../../../assets/js/http'
  import fomrMixin from '../../../../assets/js/form_com'
  import _ from 'lodash'
  import _g from '@/assets/js/global'
  import { Button, Form, FormItem, Input, Select, Option, Radio, RadioGroup } from 'element-ui'
  Vue.use(Button)
  Vue.use(Form)
  Vue.use(FormItem)
  Vue.use(Input)
  Vue.use(Select)
  Vue.use(Option)
  Vue.use(Radio)
  Vue.use(RadioGroup)

  export default {
    data() {
      return {
        loading: false,
        id: null,
        form: {
          title: '',
          pid: '',
          url: '',
          module: '',
          menu: '',
          sort: ''
        },
        show_url: true,
        options: [{ id: 0, title: '无' }],
        rules: {
          title: [
            { required: true, message: '请输入菜单标题' }
          ],
          module: [
            { required: true, message: '请填写菜单模块' }
          ],
          pid: [
            { type: 'number', required: true, message: '请选择上级菜单' }
          ]
        }
      }
    },
    methods: {
      selectPid() {
        if (this.form.pid == 0) {
          // 新模块要填module
          this.form.module = ''
          this.show_url = false
        } else {
          // 旧模块用父module
          let i = 0
          for (i = 0; i < this.options.length; i++) {
            if (this.options[i].menu_id == this.form.pid) {
              this.form.module = this.options[i].module
              this.show_url = true
              if (this.options[i].pid == 0) {
                this.show_url = false
              }
              break
            }
          }
        }
      },
      edit(form) {
        if (this.$route.params.id) {
          this.loading = true
          this.$refs.form.validate((pass) => {
            if (pass) {
              this.isLoading = true
              this.apiPut('admin/menu/', this.id, this.form).then((res) => {
                this.loading = false
                this.handelResponse(res, (data) => {
                  this.isLoading = false
                  _g.toastMsg('success', '编辑成功')
                  setTimeout(() => {
                    this.goback()
                  }, 1500)
                }, () => {
                  this.isLoading = false
                })
              })
            }
          })
        } else {
          this.$refs.form.validate((pass) => {
            if (pass) {
              this.isLoading = true
              this.apiPost('admin/menu', this.form).then((res) => {
                this.handelResponse(res, (data) => {
                  this.isLoading = false
                  _g.clearVuex('setRules')
                  _g.toastMsg('success', '添加成功')
                  setTimeout(() => {
                    this.goback()
                  }, 1500)
                }, () => {
                  this.isLoading = false
                })
              })
            }
          })
        }
      },
      goback() {
        this.$router.go(-1)
      },
      getMenus() {
        this.apiGet('admin/menu', { 'limit': 500 }).then((res) => {
          this.handelResponse(res, (data) => {
            let array = []
            _(data.list).forEach((res) => {
              if (!res.url) {
                array.push(res)
              }
            })
            this.options = this.options.concat(array)
          })
        })
      }
    },
    created() {
      this.getMenus()
      if (this.$route.params.id) {
        this.id = this.$route.params.id
        this.apiGet('admin/menu/' + this.id).then((res) => {
          this.handelResponse(res, (data) => {
            this.form = data
          })
        })
      }
    },
    components: {

    },
    mixins: [http, fomrMixin]
  }
</script>
